package com.hyy.provider.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.hyy.api.domain.customer.vo.CstmCustomerVo;
import com.hyy.api.domain.provider.bo.ProvProviderBo;
import com.hyy.api.domain.provider.vo.ProvProviderVo;
import com.hyy.common.core.domain.R;
import com.hyy.common.core.validate.AddGroup;
import com.hyy.common.core.validate.EditGroup;
import com.hyy.common.excel.utils.ExcelUtil;
import com.hyy.common.idempotent.annotation.RepeatSubmit;
import com.hyy.common.log.annotation.Log;
import com.hyy.common.log.enums.BusinessType;
import com.hyy.common.mybatis.core.page.PageQuery;
import com.hyy.common.mybatis.core.page.TableDataInfo;
import com.hyy.common.web.core.BaseController;
import com.hyy.provider.service.IProvProviderService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 上游供应商
 *
 * @author ruoyi
 * @date 2025-05-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/provider/provider")
public class ProvProviderController extends BaseController {

    private final IProvProviderService provProviderService;

    /**
     * 查询上游供应商列表
     */
    @SaCheckPermission("provider:provider:list")
    @GetMapping("/list")
    public TableDataInfo<ProvProviderVo> list(ProvProviderBo bo, PageQuery pageQuery) {
        return provProviderService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询所有上游供应商列表
     */
    @SaCheckPermission("provider:provider:list")
    @GetMapping("/listAll")
    public List<ProvProviderVo> listAll() {
        return provProviderService.queryList(new ProvProviderBo());
    }

    /**
     * 导出上游供应商列表
     */
    @SaCheckPermission("provider:provider:export")
    @Log(title = "上游供应商", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ProvProviderBo bo, HttpServletResponse response) {
        List<ProvProviderVo> list = provProviderService.queryList(bo);
        ExcelUtil.exportExcel(list, "上游供应商", ProvProviderVo.class, response);
    }

    /**
     * 获取上游供应商详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("provider:provider:query")
    @GetMapping("/{id}")
    public R<ProvProviderVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(provProviderService.queryById(id));
    }

    /**
     * 新增上游供应商
     */
    @SaCheckPermission("provider:provider:add")
    @Log(title = "上游供应商", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ProvProviderBo bo) {
        return toAjax(provProviderService.insertByBo(bo));
    }


    /**
     * 获取新客户账号
     */
    @SaCheckPermission("provider:provider:add")
    @RepeatSubmit()
    @GetMapping("/new")
    public R<ProvProviderVo> newProvider() {
        return  R.ok(provProviderService.newProvider());
    }


    /**
     * 修改上游供应商
     */
    @SaCheckPermission("provider:provider:edit")
    @Log(title = "上游供应商", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ProvProviderBo bo) {
        return toAjax(provProviderService.updateByBo(bo));
    }

    /**
     * 删除上游供应商
     *
     * @param ids 主键串
     */
    @SaCheckPermission("provider:provider:remove")
    @Log(title = "上游供应商", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(provProviderService.deleteWithValidByIds(List.of(ids), true));
    }
}
